
import 'package:flutter/material.dart';

import '../config/string.dart';

enum ViewState { loading, done, error }
class LoadingStateWidget extends StatelessWidget {
  final ViewState viewState;
  final VoidCallback retry;
  final Widget child;

  const LoadingStateWidget({Key? key, required this.viewState, required this.retry, required this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (viewState == ViewState.loading) {
      return _loadView;
    } else if (viewState == ViewState.error) {
      return _errorView;
    } else {
      return child;
    }
  }

  Widget get _errorView {
    return Center(
      // 类似LinearLayout
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            'images/ic_error.png',
            width: 100,
            height: 100,
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
            child: Text(
              AppString.net_request_fail,
              style: TextStyle(color: Color(0xff9a9a9a), fontSize: 18),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 8),
            child: OutlinedButton(
              onPressed: retry,
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.white),
                  overlayColor: MaterialStateProperty.all(Colors.black12)),
              child: const Text(
                AppString.reload_again,
                style: TextStyle(color: Colors.black87),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget get _loadView {
    return const Center(
      child: CircularProgressIndicator(),
    );
  }
}